//
//  RewardSumMoneyVC.m
//  leyitui
//
//  Created by 王大江 on 17/9/25.
//  Copyright © 2017年 xinleyi. All rights reserved.
//

#import "RewardSumMoneyVC.h"
#import "LXBannerView.h"
#import "RewardMoneyCell.h"
#import "IssueRewardVC.h"
#import "ArticleModel.h"

static NSString* const kRewardMoneyIdentifier = @"RewardMoneyCell";
static double const kTimeOutInterval = 10.0;

@interface RewardSumMoneyVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *bannerView;
@property (weak, nonatomic) IBOutlet UITableView *mainTV;

@property(nonatomic,strong) NSArray *articleModelArr;

+(AFHTTPSessionManager *)manager2;

@end

@implementation RewardSumMoneyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViews];
    [self queryArticleCardDatas];
}

-(void)setSubViews {
    LXBannerView *lXBannerView = [[LXBannerView alloc] initWithLocalImageNames:@[@"temp_img_01",@"temp_img_02",@"temp_img_03",@"temp_img_04",@"temp_img_05"] viewFrame:CGRectMake(0, 0, ScreenWidth, self.bannerView.height) duration:2 clickCompliment:^(NSInteger index) {
        
    }];
    [self.bannerView addSubview:lXBannerView];
    CGFloat bannerWidth = ScreenWidth;
    CGFloat bannerHeight = self.bannerView.height;
    UIButton *issuebtn = [[UIButton alloc] initWithFrame:CGRectMake(bannerWidth - 60, bannerHeight*0.7, 60, 25)];
    issuebtn.backgroundColor = [UIColor redColor];
    issuebtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [issuebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [issuebtn setTitle:@"发布悬赏" forState:UIControlStateNormal];
    [issuebtn addTarget:self action:@selector(issueReward) forControlEvents:UIControlEventTouchUpInside];
    [self.bannerView addSubview:issuebtn];
    [self.mainTV registerNib:[UINib nibWithNibName:@"RewardMoneyCell" bundle:nil] forCellReuseIdentifier:kRewardMoneyIdentifier];
    
    
}

-(void)queryArticleCardDatas {
    NSMutableDictionary *para = [[NSMutableDictionary alloc] init];
    para[@"id"] = @"2";
    para[@"page"] = @"1";
    para[@"method"] = @"method";
    
//    [XYNetworking POST:@"wechatlist" baseURL:LYT_URL_2001 params:para success:^(NSURLSessionDataTask *task, id responseObject) {
////        NSDictionary *dict = (NSDictionary*)responseObject;
//        NSArray *articleModels = [ArticleModel mj_objectArrayWithKeyValuesArray:responseObject];
////        if ([dict[@"code"] intValue] == 0) {
////        }
//        NSMutableArray *models = [NSMutableArray arrayWithCapacity:articleModels.count];
//        for (ArticleModel *article in articleModels) {
//            [models addObject:article];
//        }
//        self.articleModelArr = [models copy];
//        
//    } fail:^(NSURLSessionDataTask *task, NSError *error) {
//        [MBProgressHUD showError:@"请检查网络"];
//    }];
    
    // get请求也可以直接将参数放在字典里，AFN会自己讲参数拼接在url的后面，不需要自己凭借
    
    // 创建请求类
    AFHTTPSessionManager *afHttpSessionManager = [AFHTTPSessionManager manager];
    NSDictionary *params = @{
                             @"id" : @"2",
                             @"page" : @"1",
                             @"method":@"method"
                             };
    [afHttpSessionManager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    afHttpSessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
//    afHttpSessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
//    afHttpSessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    /* 使用 */
    [afHttpSessionManager setSecurityPolicy:securityPolicy];
    afHttpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [afHttpSessionManager GET:@"http://192.168.16.127:8801/api/notToken" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *strData = responseObject;
//        NSString *resultStr =  [[NSString alloc]initWithData:strData encoding:NSUTF8StringEncoding];
//        NSArray *dict = (NSArray*)responseObject;
        //系统自带JSON解析
        NSDictionary *mydict  = [NSJSONSerialization JSONObjectWithData:strData options:NSJSONReadingMutableLeaves error:nil];
        NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:mydict[@"data"]];
        NSLog(@"get success:%@",[array[0] title]);
      
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"get failure:%@",error);
    }];
    
    
    
    
}

#pragma - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    return self.articleModelArr.count;
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RewardMoneyCell *rewardMoneyCell = [tableView dequeueReusableCellWithIdentifier:kRewardMoneyIdentifier forIndexPath:indexPath];
//    [rewardMoneyCell setArticleModel:self.articleModelArr[indexPath.row]];
    
    return rewardMoneyCell;
}

-(void)issueReward {
    IssueRewardVC *issueRewardVC = [[IssueRewardVC alloc] init];
    [self.navigationController pushViewController:issueRewardVC animated:YES];
}




@end
